package org.pzy.springboot.service;

import org.apache.poi.xwpf.usermodel.*;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Service
public class WordExportService {

    public void exportWordWithTable(ServletOutputStream outputStream, List<String[]> data) throws IOException {
        // 创建一个新的 Word 文档
        XWPFDocument document = new XWPFDocument();
        // 添加标题
        XWPFParagraph title = document.createParagraph();
        title.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun titleRun = title.createRun();
        titleRun.setBold(true);
        titleRun.setFontSize(16);
        titleRun.setText("一、物业承接查验移交资料清单");
        // 添加表格
        XWPFTable table = document.createTable(data.size(), data.get(0).length);
        // 填充表格数据
        for (int i = 0; i < data.size(); i++) {
            String[] row = data.get(i);
            XWPFTableRow tableRow = table.getRow(i);
            for (int j = 0; j < row.length; j++) {
                tableRow.getCell(j).setText(row[j]);
            }
        }
        // 保存文档
        document.write(outputStream);
        // 关闭文档
        document.close();
    }
}